// components/play-btn/play-btn.js
import { submitOrders } from "../../server/orders"
import { checkPaly, getPaly } from "../../server/pay";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderId: {
      type: Number,
      value: 0
    },
    from: {
      type: String,
      value: "info"
    },
    addressId: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    qr_code_url: null,
    toCheckPaly: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toSubmitOrders() {
      let data = {
        type: "aliyun"
      }
      // console.log(data);
      // submitOrders(data).then(orders => {
      //   let data = {
      //     type: "aliyun"
      //   }
      // console.log(this.data.orderId, data);
      getPaly(this.data.orderId, data).then(paly => {
        // console.log(paly.qr_code_url);
        this.setData({
          qr_code_url: paly.qr_code_url,
          show: true
        })
        let toCheckPaly = setInterval(() => {
          checkPaly(this.data.orderId).then((res) => {
            if (res == 2) {
              clearInterval(toCheckPaly)
              wx.redirectTo({
                url: '/pages/paly_sucess/paly_sucess?ordersId=' + this.data.orderId,
              })
            }
          })
        }, 3000)
        this.setData({
          toCheckPaly
        })
      })

      // })
    },
    toOrdersInfo() {
      clearInterval(this.data.toCheckPaly)
      if (this.data.from == "info") {
        this.setData({
          show: false
        })
      } else {
        wx.redirectTo({
          url: '/pages/orders_info/orders_info?ordersId=' + this.data.orderId,
        })
      }
    }
  }
})
